package com.fgw.service.impl;

import com.fgw.common.Const;
import com.fgw.common.ServerResponse;
import com.fgw.dao.WorktrendMapper;
import com.fgw.pojo.Worktrend;
import com.fgw.service.IWorktrendService;
import com.fgw.util.DateTimeUtil;
import com.fgw.vo.WorktrendListVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by lyj on 2017/9/8.
 */
@Service("iWorktrendService")
public class WorktrendServiceImpl implements IWorktrendService {

    @Autowired
    private WorktrendMapper worktrendMapper;

    /**
     * 显示发改委的工作动态
     * @return
     */
    public ServerResponse<WorktrendListVo> getWorktrendList(){

        List<Worktrend> worktrendList=worktrendMapper.listNeedWorktrend();

       /* List<Worktrend> arrayListWorktrend=new ArrayList<>();

        for (Worktrend list : worktrendList){
            Worktrend temp = new Worktrend();
            temp.setWtId(list.getWtId());
            temp.setTitle(list.getTitle());
            temp.setSoureCrmpy(list.getSoureCrmpy());
            temp.setWritedate_v(DateTimeUtil.dateToStr(list.getWritedate(),"MM-dd"));
            temp.setPicture(list.getPicture());
            arrayListWorktrend.add(temp);
        }*/
       List<Worktrend> arrayListWorktrend=getNeedWorktrend(worktrendList);

        WorktrendListVo worktrendListVo = new WorktrendListVo();
        worktrendListVo.setWorktrendList(arrayListWorktrend);

        return ServerResponse.createBySuccess(worktrendListVo);

    }

    /**
     * 后台分页显示工作动态列表
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public ServerResponse<PageInfo> getNeedWorktrendList(int pageNum, int pageSize) {

        //分页开始
        PageHelper.startPage(pageNum,pageSize);

        List<Worktrend> worktrendList=worktrendMapper.listWorktrendAll();

       /* List<Worktrend> arrayListWorktrend=new ArrayList<>();

        //显示需要的参数
       for (Worktrend worktrend:worktrendList){
           Worktrend temp=new Worktrend();
           temp.setWtId(worktrend.getWtId());
           temp.setTitle(worktrend.getTitle());
           temp.setSoureCrmpy(worktrend.getSoureCrmpy());
           temp.setSoureDep(worktrend.getSoureDep());
           temp.setWritedate_v(DateTimeUtil.dateToStr(worktrend.getWritedate(),"yyyy-MM-dd"));
           temp.setStateZt(worktrend.getStateZt());
           temp.setZtEnddate(worktrend.getZtEnddate());
           arrayListWorktrend.add(temp);
       }*/

       List<Worktrend> arrayListWorktrend=getNeedWorktrend(worktrendList);

        //pageHelper-收尾
        PageInfo pageResult=new PageInfo(worktrendList);
        pageResult.setList(arrayListWorktrend);

        return  ServerResponse.createBySuccess(pageResult);
    }


    /**
     * 后台搜索分页显示工作动态
     * @param title
     * @param soureCrmpy
     * @param wtId
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public ServerResponse<PageInfo> searchWorktred(String title, String soureCrmpy, Integer wtId, int pageNum, int pageSize) {

       PageHelper.startPage(pageNum,pageSize);

        //拼接要查询的参数
        if (StringUtils.isNotBlank(title)){
            title=new StringBuilder().append("%").append(title).append("%").toString();
        }
        if(StringUtils.isNotBlank(soureCrmpy)){
            soureCrmpy=new StringBuilder().append("%").append(soureCrmpy).append("%").toString();
        }
        //根据参数条件搜索
        List<Worktrend> worktrendList=worktrendMapper.selectByTitleAndCrmpyAndWtid(title,soureCrmpy,wtId);

       List<Worktrend> arrayListWorktrend=getNeedWorktrend(worktrendList);

       PageInfo pageInfo=new PageInfo(worktrendList);
        pageInfo.setList(arrayListWorktrend);

        return ServerResponse.createBySuccess(pageInfo);
    }

    /**
     * 后台工作状态是否置顶
     * @param wtId
     * @return
     */
    @Override
    public ServerResponse selectOrState(Integer wtId) {
        //查询置顶状态
       int state=worktrendMapper.selectState(wtId);
        if(state==Const.state.CHECKED){
            state=Const.state.UN_CHECKED;
        }else if (state==Const.state.UN_CHECKED){
            state=Const.state.CHECKED;
        }
        //更新置顶状态
      worktrendMapper.updatedStates(wtId,state);

       return ServerResponse.createBySuccess();
    }




















    /**
     * 封装参数
     * @param worktrendList
     * @return
     */
    public  List<Worktrend> getNeedWorktrend(List<Worktrend> worktrendList){

        List<Worktrend> arrayListWorktrend=new ArrayList<>();

        //显示需要的参数
        for (Worktrend worktrend:worktrendList){
            Worktrend temp=new Worktrend();
            temp.setWtId(worktrend.getWtId());
            temp.setSoureCrmpy(worktrend.getSoureCrmpy());
            temp.setSoureDep(worktrend.getSoureDep());
            temp.setTitle(worktrend.getTitle());
            temp.setSoureCrmpy(worktrend.getSoureCrmpy());
            temp.setSoureDep(worktrend.getSoureDep());
            temp.setWritedate_v(DateTimeUtil.dateToStr(worktrend.getWritedate(),"yyyy-MM-dd"));
            temp.setStateZt(worktrend.getStateZt());
            arrayListWorktrend.add(temp);
        }

        return arrayListWorktrend;
    }
















}
